package com.tcl.aota.admin.controller;

import javax.annotation.Resource;

import com.tcl.aota.admin.dto.DnStaParamDTO;
import com.tcl.aota.cache.redis.utils.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.tcl.aota.admin.dto.Result;
import com.tcl.aota.common.constants.Constants;
import com.tcl.aota.manage.AppLogManage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StatisticsController {

    @Resource(name = "appLogManage")
    private AppLogManage appLogManage;

    @ResponseBody
    @RequestMapping(value = "/statistics/static", method = RequestMethod.GET)
    public Result staticSta() {
        return Result.get(appLogManage.getAll(), Constants.Common.SUCCESS);
    }

    //	@ResponseBody
    //	@RequestMapping(value = "/statistics/dynamic/{type}/{day}/{percent}", method = RequestMethod.GET)
    //	public Result dynamicSta(@PathVariable int type, @PathVariable int day,
    //			@PathVariable float percent) {
    //		return Result.get(appLogManage.statistics(type, day, percent),
    //				Constants.Common.SUCCESS);
    //	}

    /**
     * 按时间或者app的pageView量，下载量，安装量，更新量分组查询
     *
     * @param type：1=按appName,2=TOTAl,3:IMEI
     * @param pageNo：查询页码
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/statistics/dynamic/{type}/{pageNo}", method = RequestMethod.POST)
    public Result dynamicSta(@PathVariable int type, @PathVariable int pageNo, @RequestBody DnStaParamDTO param) {
        Map<String, Object> conditions = param.toMap();
        conditions.put("start", (pageNo - 1) * Constants.Common.PAGE_SIZE);
        conditions.put("pageSize", Constants.Common.PAGE_SIZE);
        Map<String, Object> resultMap = new HashMap<>();
        List<Map<String, Object>> data = null;
        int total = 0;
        switch (type) {
            case 1:
                total = appLogManage.countAppGroupByAppIdBetweenDate(conditions);
                data = appLogManage.selectAppGroupByAppIdBetweenDate(conditions);
                break;
            case 2:
                int statType = param.getType();
                if (statType == 1) {
                    total = appLogManage.countAppGroupByDateBetweenDate(conditions);
                    data = appLogManage.selectAppGroupByDateBetweenDate(conditions);
                } else if (statType == 2) {
                    total = appLogManage.countAppImeiNumGroupByDate(conditions);
                    data = appLogManage.selectAppImeiNumGroupByDate(conditions);
                }
                break;
            default:
                break;
        }
        resultMap.put("list", data);
        resultMap.put("total", total);
        resultMap.put("pageSize", Constants.Common.PAGE_SIZE);
        resultMap.put("pageNo", pageNo);
        return Result.get(resultMap, Constants.Common.SUCCESS);
    }
}
